var qiniu = require('./qiniu');

qiniu.conf.ACCESS_KEY = 'Xz9vwhptdWBPE7zA2E9kmmtkE6EwUst-xJWmIv32';
qiniu.conf.SECRET_KEY = 'BDC1Vrozf9eMvv9QY8X4qlDsL9rWxy_3crrvESDx';

var DOMAIN = "7xr5ip.com1.z0.glb.clouddn.com";
var BUCKET = "sagup";

String.prototype.replaceAll = function(s1,s2){
    return this.replace(new RegExp(s1,"gm"),s2);
};

/**
 * List All Qiniu Files
 * @param callback
 * @param errback
 */
function listQiniuFiles(callback, errback) {
    qiniu.rsf.listPrefix(BUCKET, "", null, 0, function(err, ret) {
        if (!err) {
            // process ret.marker & ret.items
            callback(ret.items)
        } else {
            errback(err)
            // http://developer.qiniu.com/docs/v6/api/reference/rs/list.html
        }
    });
}

function requestFilePath(filename) {
    var baseUrl = qiniu.rs.makeBaseUrl(DOMAIN, filename);
    var policy = new qiniu.rs.GetPolicy();
    return policy.makeRequest(baseUrl);
}

function uptoken(bucketname, key) {
    var putPolicy = new qiniu.rs.PutPolicy(bucketname);

    if (key) {
        putPolicy = new qiniu.rs.PutPolicy(bucketname + ":" + key);
    }
    //putPolicy.callbackUrl = callbackUrl;
    //putPolicy.callbackBody = callbackBody;
    //putPolicy.returnUrl = returnUrl;
    //putPolicy.returnBody = returnBody;
    //putPolicy.asyncOps = asyncOps;
    //putPolicy.expires = expires;

    return putPolicy.token();
}

/**
 * Upload a local file to Qiniu Server
 * @param localFile
 * @param key
 * @param callback
 * @param errback
 * @param cover
 */
function uploadFile(localFile, key, callback, errback, cover) {
    var extra = new qiniu.io.PutExtra();
    //extra.params = params;
    //extra.mimeType = mimeType;
    //extra.crc32 = crc32;
    //extra.checkCrc = checkCrc;

    qiniu.io.putFile(uptoken(BUCKET, cover ? key : undefined), key, localFile, extra, function(err, ret) {
        if(!err) {
            callback(ret.key, ret.hash, ret);
            // ret.key & ret.hash
        } else {
            errback(err);
            // http://developer.qiniu.com/docs/v6/api/reference/codes.html
        }
    });
}

function uploadBuf(body, key, callback) {
    var extra = new qiniu.io.PutExtra();
    //extra.params = params;
    //extra.mimeType = mimeType;
    //extra.crc32 = crc32;
    //extra.checkCrc = checkCrc;

    qiniu.io.put(uptoken(BUCKET), key, body, extra, function(err, ret) {
        if (!err) {
            callback(ret.key, ret.hash);
            // ret.key & ret.hash
        } else {
            callback(err);
            // http://developer.qiniu.com/docs/v6/api/reference/codes.html
        }
    });
}

function uploadArray(array, key, callback) {
    uploadBuf(new Buffer(array), key, callback);
}

function removeFile(key, callback, errback) {
    var client = new qiniu.rs.Client();
    client.remove(BUCKET, key, function(err, ret) {
        if (!err) {
            // ok
            callback();
        } else {
            errback(err);
            // http://developer.qiniu.com/docs/v6/api/reference/codes.html
        }
    })
}

module.exports = {
    requestFilePath: requestFilePath,
    uploadFile: uploadFile,
    uploadArray: uploadArray,
    listQiniuFiles: listQiniuFiles,
    uploadBuf: uploadBuf,
    removeFile: removeFile
};